package com.menglei.dailytasks.m0718;

import android.content.Context;
import android.os.Build;
import android.util.ArrayMap;
import android.util.Log;

import androidx.annotation.RequiresApi;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

public class MySharedPreferences {
    private final Context content;
    private String fileName;  //需要完整的如test.xml；而不是test
    private Map<String, Object> mMap;
    private static final String TAG = "MySharedPreferences:";

    @RequiresApi(api = Build.VERSION_CODES.N)
    public MySharedPreferences(Context context, String fileName) {
        this.content = context;
        this.fileName = fileName;
        this.mMap = new ArrayMap<>();
        try {
            xmlToString(); //如果文件存在，就将文件中的值付给mMap
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    public void putMap(Map<String, Object> mMap) throws IOException {
        this.mMap.putAll(mMap);
        String xml = XmlMapUtil.multilayerMapToXml(this.mMap, false);
        writeToXml(xml);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void putString(String key, String value) throws IOException {
        mMap.put(key, value);
        putMap(mMap);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void putInt(String key, int value) throws IOException {
        mMap.put(key, value);
        putMap(mMap);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void putBoolean(String key, boolean value) throws IOException {
        mMap.put(key, value);
        putMap(mMap);
    }

    public int getInt(String key, int Default) {
        //mMap.get(key);
        if (mMap.get(key) != null) {
            //找到了
            return Integer.parseInt(String.valueOf(mMap.get(key)));
        }
        return Default;
    }

    public String getString(String key, String Default) {
        //mMap.get(key);
        if (mMap.get(key) != null) {
            //找到了
            return String.valueOf(mMap.get(key));
        }
        return Default;
    }

    public Boolean getBoolean(String key, Boolean Default) {
        //mMap.get(key);
        if (mMap.get(key) != null) {
            //找到了
            return Boolean.parseBoolean(String.valueOf(mMap.get(key)));
        }
        return Default;
    }

    private void writeToXml(String xml) throws IOException {
        FileOutputStream out = content.openFileOutput(fileName, Context.MODE_PRIVATE);
        out.write(xml.getBytes());
        out.close();
    }


    /**
     * 将xml文件转化为string
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    public String xmlToString() throws DocumentException {
        SAXReader reader = new SAXReader();
        File file = new File(content.getFilesDir(), fileName);
        if (file.exists()) {
            Document document = reader.read(file);
            String str = document.asXML();
            Log.d(TAG, str);
            Map<String, Object> map = XmlMapUtil.multilayerXmlToMap(str);
            Map<String, Object> result = (Map<String, Object>) map.get("xml");
            mMap.putAll(result);
            Log.d(TAG, mMap.toString());
            return str;
        }
        Log.d(TAG, "该文件不存在");
        return "";
    }

}
